package cn.edu.jssvc.bluecar.asr;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.huawei.hiai.asr.AsrConstants;
import com.huawei.hiai.asr.AsrError;
import com.huawei.hiai.asr.AsrListener;

import cn.edu.jssvc.bluecar.MainActivity;

public class MyAsrListener  implements AsrListener {

    private static final String TAG = "MyAsrListener";
    private Context context;

    private AsrInit asrInit;
    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setAsrInit(AsrInit asrInit) {
        this.asrInit = asrInit;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    //    public MyAsrListener(Context context) {
//        this.context = context;
////        this.asrInit = asrInit;
//    }

    @Override
    public void onInit(Bundle params) {
//        asrInit = new AsrInit();
        Log.d(TAG, "onInit()");
        int result = params.getInt(AsrConstants.ASR_ERROR_CODE, -1);
        if (result != 0) {
            // 初始化失败
            Toast.makeText(context, "语音识别初始化失败！", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.d(TAG, "onBeginningOfSpeech() called");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.d(TAG, "onRmsChanged() called with: rmsdB = [" + rmsdB + "]");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d(TAG, "onBufferReceived() called with: buffer = [" + buffer + "]");
    }

    @Override
    public void onEndOfSpeech() {
        Log.d(TAG, "onEndOfSpeech: ");
    }

    @Override
    public void onError(int error) {
        Log.d(TAG, "onError() called with: error = [" + error + "]");
        if (error == AsrError.SUCCESS) {
            return;
        }
        if (error == AsrError.ERROR_CLIENT_INSUFFICIENT_PERMISSIONS) {
            Toast.makeText(context, "请在设置中打开麦克风权限!", Toast.LENGTH_LONG).show();
        }
        setBtEnabled(true);
    }

    @Override
    public void onRecordStart() {
        Log.d(TAG, "onRecordStart");
    }

    @Override
    public void onRecordEnd() {
        Log.d(TAG, "onRecordEnd");
        if (MainActivity.bluetoothUtils.isConnected()) {
            MainActivity.bluetoothUtils.write("ONF");
        }
    }

    @Override
    public void onResults(Bundle results) {
        Log.d(TAG, "onResults() called with: results = [" + results + "]");

        asrInit.stopListening();
        asrInit.getOnResult(results, AsrConstants.RESULTS_RECOGNITION);
        setBtEnabled(true);
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d(TAG, "onPartialResults() called with: partialResults = [" + partialResults + "]");
        asrInit.getOnResult(partialResults, AsrConstants.RESULTS_PARTIAL);
    }

    @Override
    public void onEnd() {
        Log.d(TAG, "onEnd()");
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d(TAG, "onEvent() called with: eventType = [" + eventType + "], params = [" + params + "]");
    }

    @Override
    public void onLexiconUpdated(String s, int i) {
        Log.d(TAG, "onLexiconUpdated() called with: lexiconName = [" + s + "], error = [" + i + "]");
        if (i == 0) {
            Toast.makeText(context, s + "导词成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, s + "导词失败", Toast.LENGTH_LONG).show();
        }
    }

    void setBtEnabled(boolean isEnabled) {
//        view.findViewById(R.id.bt_startListen).setEnabled(isEnabled);

        if (isEnabled){
            this.handler.sendEmptyMessage(MainActivity.BT_ENABLE_TRUE);
            if (MainActivity.bluetoothUtils.isConnected()) {
                MainActivity.bluetoothUtils.write("ONF");
            }
        }else {
            this.handler.sendEmptyMessage(MainActivity.BT_ENABLE_FALSE);
        }

    }


}
